//
//  WPNewEditeSpecificCasesVCViewController.m
//  wobingwoyi
//
//  Created by WGP on 16/11/8.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPNewEditeSpecificCasesVC.h"
#import "WPNewFirstTableViewController.h"
#import "WPNewEditeCasesCVC.h"
#import "WPNewSecondTVC.h"
#import "WPNewThirdTVC.h"
#import "WPMRModel.h"
#import "WPFirstTableHeaderView.h"

@interface WPNewEditeSpecificCasesVC ()<UICollectionViewDelegate,UICollectionViewDataSource,WPFirstTableHeaderViewDelegate>

@property(nonatomic,strong)WPNewFirstTableViewController *tvc;

@end

@implementation WPNewEditeSpecificCasesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:GLobalFontColor forState:UIControlStateNormal];
    [saveBtn setTitleColor:GloballightFontColor forState:UIControlStateHighlighted];
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [saveBtn.titleLabel sizeToFit];
    [saveBtn addTarget:self action:@selector(saveBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.navBarView addSubview:saveBtn];
    
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.navBarView);
        make.right.equalTo(self.navBarView).offset(-10);
    }];
    
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleView.mas_bottom).offset(1);
    }];
    [self.lineView removeFromSuperview];
    
    self.view.backgroundColor = GlobalBgColor;
    self.titleLabel.text = self.model.name.length ? self.model.name : @"病例标题";
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerClass:[WPNewEditeCasesCVC class] forCellWithReuseIdentifier:@"WPNewEditeCasesCVC"];
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model == nil ? 0 : 3;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    WPNewEditeCasesCVC *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WPNewEditeCasesCVC" forIndexPath:indexPath];
    //    移除上一个控制器
    [cell.tvc.view removeFromSuperview];
    
    if (indexPath.item == 0)
    {
        WPNewFirstTableViewController *tvc = [WPNewFirstTableViewController new];
        //    传递控制器
        cell.tvc = tvc;
        self.tvc = tvc;
        tvc.model = self.model;
        tvc.headerView.delegate = self;
        //    如果一个控制器的View作为另一个控制器的子控件那么这个控制器也应该作为另一个控制器的子控制器
        [self addChildViewController:tvc];
    }
    else if(indexPath.item == 1)
    {
        WPNewSecondTVC *tvc = [[WPNewSecondTVC alloc] initWithStyle:UITableViewStylePlain];
        tvc.model = self.model;
        //    传递控制器
        cell.tvc = tvc;
        //    如果一个控制器的View作为另一个控制器的子控件那么这个控制器也应该作为另一个控制器的子控制器
        [self addChildViewController:tvc];
    }
    else
    {
        WPNewThirdTVC *tvc = [WPNewThirdTVC new];
        tvc.model = self.model;
        cell.tvc = tvc;
    
        [self addChildViewController:tvc];
    }
    return cell;
}

#pragma mark - WPFirstTableHeaderViewDelegate代理方法
-(void)WPFirstTableViewHeaderViewSaveSuccessBlock:(WPFirstTableHeaderView *)view
{
    //只有在分享编辑保存的时候才返回
    if (self.isBack)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)setModel:(WPMRModel *)model
{
    _model = model;
    
}

-(void)saveBtnDidClick:(UIButton *)btn
{
    self.tvc.baseInfoEditeBlock();
}

-(void)titleBtnDidClick:(WPBtnNoHighLight *)btn
{
    [super titleBtnDidClick:btn];
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:btn.tag-1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    [self titleBtnDidClick:[self.view viewWithTag:index+1]];
}

//滚动的时候隐藏键盘
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
